package com.tuyan.aixiang.component;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.amap.api.services.weather.LocalWeatherLive;
import com.jaychan.view.MultipleTextView;
import com.tuyan.aixiang.R;
import com.tuyan.aixiang.common.lang.VoidFunction;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WeatherPanel extends LinearLayout {

    @BindView(R.id.tv_temperature)
    TextView tvTemperature;

    @BindView(R.id.tv_weather_name)
    TextView tvWeatherName;

    @BindView(R.id.mtv_humidity)
    MultipleTextView mtvHumidity;

    @BindView(R.id.tv_location)
    TextView tvLocation;

    public WeatherPanel(Context context) {
        this(context, null);
    }

    public WeatherPanel(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WeatherPanel(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        View contentView = LayoutInflater.from(context).inflate(R.layout.weather_panel, this);
        ButterKnife.bind(contentView, this);

    }

    public void setRefreshCallback(VoidFunction function) {
        setOnClickListener(v -> {
            function.call();
        });
    }

    public void loadWeather(LocalWeatherLive liveData) {
        if(null == liveData) {
            tvTemperature.setText("-");
            tvWeatherName.setText("-");
            mtvHumidity.setContentText("-");
            tvLocation.setText("-");
        } else {
            tvTemperature.setText(liveData.getTemperature());
            tvWeatherName.setText(liveData.getWeather());
            mtvHumidity.setContentText(liveData.getHumidity());
            tvLocation.setText(liveData.getCity());
        }

    }

}
